<?php


namespace app\manage\controller;


use app\common\controller\LockService;
use think\Log;

class AppConfig extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        // 查询条件start
        $org_id = input('org_id')?input('org_id'):0;
        $map = [];
        if($org_id != null){
            $map['org_id'] = $org_id;
        }
        // 查询条件end

        $infos = model('AppConfig')->getInfosByMap($map, '', false);
        $orgs = model('OrgGroup')->getListByMap([], true, '', 'id asc');

        foreach ($infos as $k=>$v){
            $org = model('OrgGroup')->getInfoByMap(['id'=>$v['org_id']], '', false);
            $infos[$k]['org_name'] = $org['name'];
        }

        return $this->fetch('',[
            'infos' => $infos,
            'org_id' => $org_id,
            'orgs' => $orgs
        ]);
    }

    public function edit()
    {
        $id = input('id');
        if(!$id) return self::showReturnCode(1002,'id 错误');
        $user = model('AppConfig')->getInfoByMap(['id'=>$id]);
        return self::showJsonReturnCodeData('1001',$user,'');
    }

    public function save()
    {
        if(request()->isAjax()){
            try{

                $id = input('post.id');

                $save_data['app_name'] = input('post.app_name');
                $save_data['remark'] = input('post.remark');

                if(isset($id) && $id){
                    $save_data['id'] = $id;
                }else{
                    $save_data['org_id'] = input('post.org_id');
                    $save_data['admin_id'] = $this->uid;
                    $save_data['api_key'] = input('post.api_key');
                }
                $result = $this->editData(false,'AppConfig','AppConfig',$save_data);
                if($result['code'] == 1001){
                    if(isset($id) && $id){
                        $result['msg'] = '修改应用成功';
                        $this->RecordSystemLog($this->uid,$this->behavior,'修改应用【'.$save_data['app_name'].'】成功');
                    }else{
                        $result['msg'] = '添加应用成功';
                        $this->RecordSystemLog($this->uid,$this->behavior,'添加应用【'.$save_data['app_name'].'】成功');
                    }
                }

                return $result;
            }catch (\Exception $e){
                Log::error($e->getMessage());
                return self::showReturnCode(1011,$e->getMessage());
            }
        }else{
            return self::showReturnCode(1002);
        }
    }

    public function refresh()
    {
        $api_key = md5(random_str(8));
        return self::showJsonReturnCodeData('1001',['api_key'=>$api_key],'');
    }

    public function status()
    {
        if($this->request->isAjax()){
            $id = input('post.id');
            $res = model('AppConfig')->save(['status'=>input('post.status')],['id'=>$id]);
            if($res){

                $app = model('AppConfig')->getInfoByMap(['id'=>$id],false,'',false);
                $redis_api_key = 'apiKey:'.$app['org_id'].':'.$app['id'];
                $redis = (new LockService())::getRedisConn();
                $redis->set($redis_api_key,json_encode($app));

                $this->RecordSystemLog($this->uid,$this->behavior,'状态操作成功,操作ID: '.$id);
                return $this->showReturnCode(1001);
            }else{
                return $this->showReturnCode(1009,'操作失败');
            }
        }else{
            return $this->showReturnCode(1002);
        }
    }
}